package com.hfzy.ihk.web.userCenter.biz;

import com.hfzy.ihk.facade.userCenter.entity.Priv;
import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import com.hfzy.ihk.web.userCenter.mapper.PrivHasResourceMapper;
import com.hfzy.ihk.web.userCenter.mapper.PrivMapper;
import com.hfzy.ihk.web.userCenter.vo.PrivDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by YANFA on 2019/2/1.
 */
@Component
@Slf4j
public class PrivBiz {

    @Autowired
    PrivMapper privMapper ;

    @Autowired
    PrivHasResourceMapper privHasResourceMapper;

    /**
     * 删除权限的同时删除相关的关系
     * @param id
     */
    @Transactional
    public void deletePriv(Long id) {

        privHasResourceMapper.deletePrivSource(id);

        privHasResourceMapper.deleteUserHasPrivByPrivId(id);

        privHasResourceMapper.deleteRoleHasPrivByPrivId(id);

        privMapper.deleteById(id);

        log.info("删除权限id:{}", id);
    }

    /**
     * 保存权限
     * @param privDto
     */
    @Transactional
    public void savePriv(PrivDto privDto){
        List<Long> resourceIds = privDto.getResourceIds();
        if(resourceIds!=null) {
            resourceIds.remove(0L);//排除根节点
        }
        if(privDto.getId()!=null){
            updatePriv(privDto,resourceIds);
        }else {
            savePriv(privDto,resourceIds);
        }

    }


    private void updatePriv(Priv priv, List<Long> resourceIds ){
        Priv p = privMapper.getPriv(priv.getPrivName());
        if (p != null&&priv.getId()!=p.getId()) {
            throw new IllegalArgumentException(p.getPrivName() + "已存在");
        }
        UserInfo userInfo = UserUtil.getLoginUser();
        if(userInfo!=null){
            priv.setModTime(new Date());
            priv.setModUserId(userInfo.getId());
        }
        privMapper.updateById(priv);

        //插入权限和菜单关系 ---先删除在保存关系
        privHasResourceMapper.deletePrivSource(priv.getId().longValue());

        if (!CollectionUtils.isEmpty(resourceIds)) {
            privHasResourceMapper.savePrivHasResource(priv.getId().longValue(), resourceIds);
        }
        log.info("更新权限{}",priv.getPrivName());

    }


    private void savePriv(Priv priv, List<Long> resourceIds ){
        Priv p = privMapper.getPriv(priv.getPrivName());
        if (p != null) {
            throw new IllegalArgumentException(p.getPrivName() + "已存在");
        }
        UserInfo userInfo = UserUtil.getLoginUser();

        if(userInfo!=null){
            priv.setCreateTime(new Date());
            priv.setCreateUserId(userInfo.getId());
            priv.setEnabled(true);
        }
        privMapper.insert(priv);

        //插入权限和菜单关系
        if (!CollectionUtils.isEmpty(resourceIds)) {
            privHasResourceMapper.savePrivHasResource(priv.getId().longValue(), resourceIds);
        }
        log.info("新增权限{}",priv.getPrivName());

    }
}


